import { ref } from 'vue'
import { adapter, request } from '@/request'
import { priceSheetByPost } from '@/core/api/priceSheet'
import { message } from 'ant-design-vue'
import is from '@/utils/is'
import type { TypeEnum, VersionTypeEnum, LandingPageEnum, IsFromHjzwEnum } from '@/core/enums/type'
import type { ApproveStatusEnum } from '@/core/enums/status'
import type { ProductEnum } from '@/core/enums/platform'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { PriceKeyword } from '@/core/interfaces/keyword'
import type { ProxyPack, SellerPack } from '@/core/interfaces/base'
import type { Combo, Price } from '@/core/interfaces/combo'

export interface PriceSheetCreator extends SellerPack, ProxyPack, Combo {
  type?: TypeEnum
  productId: ProductEnum
  priceAskWords: PriceKeyword[]
  // oldOrderId?: PriceSheet['oldOrderId']
  approveStatus: ApproveStatusEnum
}

export interface HjbwChannelPrice extends PriceSheetCreator, Price {
  productId: ProductEnum.HJBW
  type: TypeEnum
  versionType: VersionTypeEnum
  areas?: string
  onLinePeriod?: string
  shopType?: LandingPageEnum
  hzTransferCode?: IsFromHjzwEnum
  hzNotes?: string
  machineExamine?: number
  moveUp: number
  addWord: number
  // 新增字段
  premiumPrice: Price['premiumPrice']
  premiumRatio: Price['premiumRatio']
  marketPrice: Price['price']
}

export default function createPriceSheet() {
  const service = adapter<RequestWrapper<any>>(request)
  const loading = ref(false)
  const response = ref<any>()
  const fetch = (data: Partial<HjbwChannelPrice>) => {
    const { url, method } = priceSheetByPost()
    loading.value = true

    return service({
      config: {
        url,
        method,
        data
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        if (is.error(err)) {
          message.error(err.message)
        } else {
          message.error('请求失败，请稍候再试...')
        }
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
